package com.corx.network.core.util;


import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @author gx
 * @version 1.0.0
 */
@Slf4j
public class JsonPathParser {

    private JsonPathParser() {
    }

    private static final Map<Class<?>, Function<Object, Object>> TYPE_CONVERTERS = new HashMap<>();


    public static Object extractWithModel(String json, String jsonPath, String fieldType, String fieldName) {

        Object document = JsonPath.parse(json).json();
        try {
            Object raw = JsonPath.read(document, jsonPath);
            return JdbcUtils.toJavaType(raw, fieldType);
        } catch (Exception e) {
            log.error("字段 [{}] 解析失败: ", fieldName, e);
            return null;
        }
    }

}
